package com.dreamlin.lindownload;

import com.dreamlin.lindownload.entity.LinTaskBean;

import java.lang.ref.WeakReference;

/**
 * @see LinDownloadTask
 * 如果LinTaskImp的实现是该类，那么存在两种情况
 * 1.新建的任务，说明taskMap中不包含
 * 2.由暂停的taskMap任务转到waitQueue
 */
public class LinDownloadTaskImp implements LinTaskImp {

    private LinTaskBean linTaskBean;
    private LinDownloadListener weakListener;
    private String url;

    public LinDownloadTaskImp(String url, LinDownloadListener listener) {
        this.url = url;
        if (LinDownload.taskMap.containsKey(url)) {
            linTaskBean = LinDownload.taskMap.get(url).getLinTaskBean();
        } else {
            linTaskBean = new LinTaskBean(url);
        }
        weakListener = listener;
    }

    public LinDownloadTaskImp(LinTaskBean linTaskBean, LinDownloadListener weakListener) {
        this.linTaskBean = linTaskBean;
        this.weakListener = weakListener;
    }

    @Override
    public LinDownloadTaskImp path(String savePath) {
        linTaskBean.setSavePath(savePath);
        return this;
    }

    public String getUrl() {
        return url;
    }

    @Override
    public LinDownloadTaskImp name(String fileName) {
        linTaskBean.setFileName(fileName);
        return this;
    }

    @Override
    public LinDownloadTaskImp threadNum(int threadNum) {
        if (threadNum > LinDownload.MAX_THREAD_NUMBER)
            threadNum = LinDownload.MAX_THREAD_NUMBER;
        linTaskBean.setThreadNum(threadNum);
        return this;
    }

    @Override
    public LinDownloadTaskImp listener(LinDownloadListener listener) {
        this.weakListener = listener;
        return this;
    }

    @Override
    public LinDownloadTaskImp callInterval(long timeMills) {
        linTaskBean.setCallInterval(timeMills);
        return this;
    }

    @Override
    public LinDownloadTaskImp retryNum(int retryNumber) {
        linTaskBean.setAutoRetry(retryNumber);
        return this;
    }

    @Override
    public LinTaskImp start() {
        if (linTaskBean != null) {
            if (LinDownload.runTaskNumber >= LinDownload.MAX_TASK_NUMBER) {
                if (!LinDownload.waitQueue.contains(url)) {
                    LinDownload.waitQueue.add(this);
                }
            } else {
                LinDownloadTask linDownloadTask = new LinDownloadTask(linTaskBean, weakListener);
                LinDownload.taskMap.put(url, linDownloadTask);
                linDownloadTask.start();
            }
        }
        return this;
    }


    @Override
    public LinTaskImp pause() {
        if (linTaskBean != null) {
            if (LinDownload.waitQueue.contains(url)) {
                linTaskBean.setState(LinDownload.STATUS_PAUSE);
            }
        }
        return this;
    }

    @Override
    public LinTaskImp cancel() {
        if (linTaskBean != null) {
            if (LinDownload.waitQueue.contains(url)) {
                LinDownload.waitQueue.remove(this);
                LinDownload.decrease();
            }
        }
        return this;
    }
}
